﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructures;
using Models;
using Newtonsoft.Json;

namespace ConcreteService
{
    public class ContractTemplateManager
    {
        public virtual string GetCurrentVersion()
        {
            return SystemConfig.Get("CurrentContractVersion");
        }

        public virtual IDictionary<FieldName, string> GetTemplateValue(ProjectContract contract)
        {
            return null;
        }
    }

    public class TemplateV1 : ContractTemplateManager
    {
        public override IDictionary<FieldName, string> GetTemplateValue(ProjectContract contract)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            ProjectService service = ServiceFactory.CreateInstance<ProjectService>();

            var project = service.Get<Project>(contract.ProjectID.ToString());

            double percentageMoney = 0;

            if (contract.AgentID > 0)
            {
                var baseData = AppConfig.GetBaseData(BaseDataFun.RebateRate, (Int32)contract.Amount);
                percentageMoney = contract.Amount * baseData.Level.ToDouble();
            }

            dic.Add("PartyA", project.CreatorType == APPRole.Company ? project.CreatorName : contract.ContractorName);
            dic.Add("PartyB", project.CreatorType == APPRole.Company ? contract.ContractorName : project.CreatorName);
            dic.Add("ProjectName", project.ProjectName);
            dic.Add("SponsorMoney", contract.Amount.ToString());
            dic.Add("PercentageMoney", percentageMoney.ToString());
            dic.Add("Amount", (contract.Amount - percentageMoney).ToString());

            var data = service.GetProjectData(project.ProjectID, contract.ContractID, FieldName.Feedback);
            var dataSup = service.GetProjectData(project.ProjectID, contract.ContractID, FieldName.Supervision);

            if (project.CreatorType == APPRole.Student)
            {
                dic.Add("Feedback", data == null ? string.Empty : data.DataValue);
                dic.Add("Supervision", dataSup == null ? string.Empty : dataSup.DataValue);
            }
            else
            {
                dic.Add("Feedback", dataSup == null ? string.Empty : dataSup.DataValue);
                dic.Add("Supervision", data == null ? string.Empty : data.DataValue);
            }

            IDictionary<FieldName, string> dicResult = new Dictionary<FieldName, string>();
            dicResult.Add(FieldName.ContractContent, JsonConvert.SerializeObject(dic));
            dicResult.Add(FieldName.PercentageMoney, percentageMoney.ToString());

            return dicResult;
        }
    }
}
